package com.ruoyi.duty.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.duty.domain.bo.DutySdpcBo;
import com.ruoyi.duty.domain.vo.DutySdpcVo;
import com.ruoyi.duty.service.IDutySdpcService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 涉毒排查
 *
 * @author xiaohao
 * @date 2023-01-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/duty/sdpc")
public class DutySdpcController extends BaseController {

    private final IDutySdpcService iDutySdpcService;

    /**
     * 查询涉毒排查列表
     */
    @GetMapping("/list")
    public TableDataInfo<DutySdpcVo> list(DutySdpcBo bo, PageQuery pageQuery) {
        return iDutySdpcService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出涉毒排查列表
     */
    @Log(title = "涉毒排查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DutySdpcBo bo, HttpServletResponse response) {
        List<DutySdpcVo> list = iDutySdpcService.queryList(bo);
        ExcelUtil.exportExcel(list, "涉毒排查", DutySdpcVo.class, response);
    }

    /**
     * 获取涉毒排查详细信息
     *
     * @param sdpcId 主键
     */
    @GetMapping("/{sdpcId}")
    public R<DutySdpcVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long sdpcId) {
        return R.ok(iDutySdpcService.queryById(sdpcId));
    }

    /**
     * 新增涉毒排查
     */
    @SaCheckPermission("duty:sdpc:add")
    @Log(title = "涉毒排查", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DutySdpcBo bo) {
        return toAjax(iDutySdpcService.insertByBo(bo));
    }

    /**
     * 修改涉毒排查
     */
    @SaCheckPermission("duty:sdpc:edit")
    @Log(title = "涉毒排查", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DutySdpcBo bo) {
        return toAjax(iDutySdpcService.updateByBo(bo));
    }

    /**
     * 删除涉毒排查
     *
     * @param sdpcIds 主键串
     */
    @SaCheckPermission("duty:sdpc:remove")
    @Log(title = "涉毒排查", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sdpcIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] sdpcIds) {
        return toAjax(iDutySdpcService.deleteWithValidByIds(Arrays.asList(sdpcIds), true));
    }
}
